﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// AdminManage 的摘要说明。
    /// </summary>
    public class AdminManage : MarshalByRefObject
    {
        private static readonly UserDal Dal = new UserDal();

        /// <summary>
        /// 进行添加管理员操作
        /// </summary>
        /// <param name="model"></param>
        [Logging("添加管理员"), ExceptionCall]
        public Result Add(User model)
        {
            Result res = new Result();
            model.IsAdmin = true;
            if (Dal.GetInfo(model.UserName) != null)
            {
                res.Errors = "用户名已经存在！";
            }
            else
            {
                model.PassWord = Hash.Md5(model.PassWord); //密码
                model.RegTime = DateTime.Now;
                new UserDal().Add(model); //添加数据库操作
            }
            return res;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="model"></param>
        /// <param name="newPSW"></param>
        [Logging("修改密码"), ExceptionCall]
        public Result ChangPSW(User model, string newPSW)
        {
            Result resu = new Result();
            model.PassWord = Hash.Md5(model.PassWord);


            User info = Dal.GetInfo(model.UserName);
            if (info == null)
            {
                resu.Errors = "管理员不存在。";
                return resu;
            }

            if (info.PassWord.Equals(model.PassWord, StringComparison.CurrentCultureIgnoreCase))
            {
                Dal.ChangPSW(model.UserName, Hash.Md5(newPSW));

                return resu;
            }
            resu.Errors = "旧密码错误！新密码不能更改。";
            return resu;
        }


        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="model"></param>
        [Logging("删除管理员"), ExceptionCall]
        public Result Delete(User model)
        {
            Result resu = new Result();
            new UserDal().Delete(model.Id);

            return resu;
        }
    }
}